﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Baseframework.Model;
using Honestysoft.Financing.Model;
using System.Data.Common;
using System.Web.SessionState;

namespace Honestysoft.Financing.WebUI.Module.Project.Json
{
    /// <summary>
    /// UploadProjectInvestigage 的摘要说明
    /// </summary>
    public class UploadProjectInvestigage : IHttpHandler, IRequiresSessionState 
    {

        public void ProcessRequest(HttpContext context)
        {
            //HttpPostedFile file = context.Request.Files[0];
            //string extName = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
            //string newFileName = context.Request.Params["FileID"];
            //file.SaveAs(context.Server.MapPath(Globals.Home + Utils.Reports_Dir_Investigate + "/" + newFileName + "." + extName));
            //Sys_Attachment obj = new Utils().GetInvestigate(newFileName);
            //obj.Extension = extName;
            //obj.Size = file.ContentLength;
            //new Honestysoft.BaseObject.BaseBusinessLogic<Sys_Attachment>().UpdateEntity(obj);

            HttpPostedFile file = context.Request.Files[0];
            string extName = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);
            string investId = context.Request.Params["FileID"];

            Honestysoft.BaseObject.BaseBusinessLogic<Sys_Attachment> bll = new BaseObject.BaseBusinessLogic<Sys_Attachment>();
            DbTransaction transaction = Honestysoft.BaseLibrary.Data.DatabaseProvider.GetDbTransaction();
            DbConnection connection = transaction.Connection;

            try
            {
                
                Sys_Attachment attInvest = new Utils().GetInvestigate(investId);
                attInvest.Extension = extName;
                attInvest.Size = file.ContentLength;
                bll.UpdateEntity(attInvest, transaction);
                file.SaveAs(context.Server.MapPath(Globals.Home + Utils.Reports_Dir_Investigate + "/" + investId + "." + extName));
                //要同时保存到项目简介里面去
                Bu_Investigate inv = new Honestysoft.BaseObject.BaseBusinessLogic<Bu_Investigate>().GetEntity(attInvest.DataID);
                Honestysoft.BaseObject.QueryParameters query = new BaseObject.QueryParameters();
                query.Add("DataID", inv.ProjectID, BaseObject.QueryParameters.OperatorType.Equal);
                query.Add("AttachmentType", ((int)Honestysoft.Financing.Model.EnumItem.ProjectDocument.Overview).ToString(), BaseObject.QueryParameters.OperatorType.Equal);
                List<Sys_Attachment> lsOverviews = bll.GetEntitys(query,transaction);
                
                if (lsOverviews != null && lsOverviews.Count == 1)
                {
                    lsOverviews[0].Extension = extName;
                    lsOverviews[0].Size = file.ContentLength;
                    bll.UpdateEntity(lsOverviews[0], transaction);
                    file.SaveAs(context.Server.MapPath(Globals.Home + Utils.Reports_Dir_Overview + "/" + lsOverviews[0].FileName + "." + extName));
                }
                else
                {
                    Sys_Attachment overivew = new Sys_Attachment();
                    overivew.AttachmentBuType = ((int)Honestysoft.Financing.Model.EnumItem.ProjectDocument.Overview).ToString();
                    overivew.CreateTime = DateTime.Now;
                    overivew.Creator = UserLoginer.LoginUser.ID;
                    overivew.DataID = inv.ProjectID;
                    overivew.Extension = extName;
                    overivew.FileName = inv.ProjectID;
                    overivew.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                    overivew.Path = Utils.Reports_Dir_Overview;
                    overivew.Size = file.ContentLength;
                    overivew.StoreType = ((int)Honestysoft.Baseframework.Model.EnumItem.StoreType.Disk).ToString();
                    bll.CreateEntity(overivew, transaction);
                    file.SaveAs(context.Server.MapPath(Globals.Home + Utils.Reports_Dir_Overview + "/" + overivew.FileName + "." + extName));
                }
                transaction.Commit();
                context.Response.Write("1");
            }
            catch (Exception ex)
            {
                transaction.Rollback();
                Honestysoft.BaseLibrary.ExceptionHandling.ExceptionHelper.ExceptionDistribute(ex, false);
                context.Response.Write("保存文件时出错！");
            }
            finally
            {
                connection.Close();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}